<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-05-14
 * Time: 15:51
 */
namespace app\admin\controller;

use think\Controller;
use think\Db;

class Agent extends Base
{

    /**
     * 空操作
     */
    public function _empty(){
        if($this->check_admin_login()){
            $this->redirect('admin/Index/index');
        }else{
            $this->redirect('admin/login');
        }
    }

    /**
     * 代理列表
     */
    public function agent_list(){
        $page_param = input('param.');
        if (isset($page_param['page']) && $page_param['page'] > 0)
            unset($page_param['page']);

        $agent_list = Db::table('yf_agent')->field('id,name,pre_commission,add_time,identification')->order('id desc')->paginate(20, false, ['query' => $page_param]);
        //分页信息
        $page = $agent_list->render();
        $agent_list = $agent_list->toArray();
        //模板变量赋值
        $this->assign('list', $agent_list['data']);
        $this->assign('total', $agent_list['total']);
        $this->assign('page', $page);
        // 渲染模板输出
        return $this->fetch();
    }


    /**
     * 店铺添加
     */
    public function agent_add(){
        if ($this->request->isPost()){
            $data['logo'] = input('param.logo',''); //店铺图标
            $data['province'] = input('param.pro',''); //省份
            $data['city'] = input('param.city',''); //城市
            $data['county'] = input('param.county',''); //地区
            $data['address'] = input('param.address',''); //详细地址
            $url= "http://restapi.amap.com/v3/geocode/geo?key=".config('map.webapi')."&address=".urlencode($data['address'])."&city=".urlencode($data['city']);
            $datas = file_get_contents($url);
            $content=strip_tags($datas);
            $content_arr=json_decode($content,true);
            if ($content_arr['infocode'] == '10000'){
                $location = explode(',',$content_arr['geocodes'][0]['location']);
                $data['lng'] = $location[0];//经度
                $data['lat'] = $location[1];//纬度
                $data['citycode'] = $content_arr['geocodes'][0]['citycode'];//所在城市代码
            }else{
                return json(['code'=>0,'msg'=>'获取地址经纬度信息有误']);
            }
            $data['shop_name'] = input('param.shop_name',''); //店铺名称
            $data['category'] = input('param.category',''); //店铺类型
            $data['is_self'] = input('param.is_self',1); //是否自营
            $data['shop_keeper'] = input('param.shop_keeper','admin');//店铺联系人 店长
            $data['phone'] = input('param.phone',0);//联系人电话
            $data['shop_phone'] = input('param.shop_phone','');//店铺座机
            $data['status'] = input('param.status',1);//店铺状态
            if ($data['lat'] == '' || $data['lng'] == '' || $data['address'] ==''){
                return json(['code'=>0,'msg'=>'店铺地址不能为空']);
            }
            if (ShopModel::shop_add($data)){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>0,'msg'=>ShopModel::getError()]);
            }

        }else{

            // 渲染模板输出
            return $this->fetch();
        }

    }



}